package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.LabelBO;
import com.junzhiit.nengji.bo.LabelListBO;
import com.junzhiit.nengji.dao.LabelDAO;
import com.junzhiit.nengji.model.Label;
import com.junzhiit.nengji.model.enums.LoginType;

@Repository
public class LabelDAOImpl extends AbstractBaseDAO<Label> implements LabelDAO{
	
	@Override
	public List<LabelListBO> findPageAll(Label label, int pageNumber, int maxPageRow){
		StringBuffer sql=new StringBuffer();
		sql.append(" select id,name,");
		sql.append(" (select group_concat(st.`name`) from system_type_label stl inner join system_type st on st.id = stl.system_type_id where stl.label_id=label.id ) system_type_name");
		sql.append(" from label");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(label.getPlatform()!=null){
			sql.append(" and platform=?");
			param.setInt(label.getPlatform());
		}
		
		return super.queryForPagingList(sql.toString(), LabelListBO.class, param, pageNumber, maxPageRow);
	}
	
	@Override
	public int getTotalCount(Label label){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from label");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(label.getPlatform()!=null){
			sql.append(" and platform=?");
			param.setInt(label.getPlatform());
		}
		
		return super.queryForInt(sql.toString(), param);
	}
	
	
	@Override
	public List<LabelBO> getAll(String shopId,List<String> systemTypeIds){
		
		
		
		StringBuffer sql=new StringBuffer();
		sql.append(" select l.name,l.id label_id,li.value,li.id from label l");
		sql.append(" left join label_items li on l.id=li.label_id");
		sql.append(" left join system_type_label stl on stl.label_id=l.id ");
		sql.append(" where l.is_delete=?");
		sql.append(" and ((l.platform=? and l.shop_id=?) ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(LoginType.SHOP.toIntType());
		param.setString(shopId);
		
		if(null!=systemTypeIds && !systemTypeIds.isEmpty()){
			sql.append("or ( l.platform=? and stl.system_type_id in (");
			param.setInt(LoginType.INTERNAL.toIntType());
			
			for(int i=0;i<systemTypeIds.size();i++){
				sql.append(" ?");
				if(i < systemTypeIds.size()-1){
					sql.append(" , ");
				}
				param.setString(systemTypeIds.get(i));
			}
			sql.append(" )) ");
		}
		
		sql.append(")");
		sql.append(" order by l.platform desc,l.created_time asc");
		
		return queryForList(sql.toString(), LabelBO.class, param);
	}
	
	@Override
	public int getIsExist(Label label){
		
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from label");
		sql.append(" where is_delete= ? ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(label.getName())){
			sql.append(" and name = ?");
			param.setString(label.getName());
		}
		
		if(StringUtils.isNotBlank(label.getShopId())){
			sql.append(" and shop_id = ?");
			param.setString(label.getShopId());
		}
		
		return super.queryForInt(sql.toString(),param);
		
	}

	@Override
	public List<LabelBO> getGoodsAll(String goodsId) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		sql.append(" select gli.id,gli.label_id labelId,l.name,li.`value` from goods_label_items gli ");
		sql.append(" left join label l on gli.label_id = l.id left join label_items li on  gli.label_items_id =  li.id ");
		sql.append(" where gli.goods_id = ? and gli.is_delete = ? and l.is_delete = ? and li.is_delete =? order by gli.sort desc ");
		param.setString(goodsId);
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		
		return queryForList(sql.toString(), LabelBO.class, param);
	}
	
	@Override
	public LabelListBO findById(String id){
		StringBuffer sql=new StringBuffer();
		sql.append(" select id,name,");
		sql.append(" (select group_concat(st.id) from system_type_label stl inner join system_type st on st.id = stl.system_type_id where stl.label_id=label.id ) system_type_id");
		sql.append(" from label");
		sql.append(" where id=?");
		
		StatementParameter param=new StatementParameter();
		param.setString(id);
		
		return query(sql.toString(), LabelListBO.class, param);
	}

	
}
